﻿package mortal.game.view.palyer.view
{
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.palyer.util.*;
    import mortal.game.view.playerSystem.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.shopMall.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class FashionActiveTipWin extends SmallWindow
    {
        private var _isSureActive:GTextFiled;
        private var _activeConsume:GTextFiled;
        private var _currOwn:GTextFiled;
        private var _autoBuy:GCheckBox;
        private var _confirmBtn:GButton;
        private var _cancelBtn:GButton;
        private var _fashionType:int;
        private static var _instance:FashionActiveTipWin;

        public function FashionActiveTipWin(param1:ILayer = null)
        {
            super(param1);
            setSize(253, 215 - 20 - 10);
            this.layer = LayerManager.fullScreenTopLayer;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._isSureActive = UIFactory.gTextField(Language.getString(41278), 60 + 10, 54 - 15, 170, 25, this, GlobalStyle.textFormatBai.setSize(14));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41279), 45 + 10, 54 - 15 + 30, 75, 25, this, GlobalStyle.textFormatHuang));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41280), 45 + 10, 54 - 15 + 60, 75, 25, this, GlobalStyle.textFormatHuang));
            this._activeConsume = UIFactory.gTextField("", 120, 54 - 15 + 30, 100, 25, this, GlobalStyle.textFormatZi);
            this._currOwn = UIFactory.gTextField("", 120, 54 - 15 + 60, 100, 25, this, GlobalStyle.textFormatZi);
            this._autoBuy = UIFactory.checkBox(Language.getString(41281), 30, 54 - 15 + 80 + 2, 70, 28, this);
            this._confirmBtn = UIFactory.gButton(Language.getString(41282), 59, 162 + 6 - 20 + 5, 61, 27, this);
            this._cancelBtn = UIFactory.gButton(Language.getString(41283), 136 + 2, 162 + 6 - 20 + 5, 61, 27, this);
            this._confirmBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._cancelBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackItemChangeHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.onPackItemChangeHandler);
            return;
        }// end function

        public function set type(param1:int) : void
        {
            this._fashionType = param1;
            this.updateInfo();
            return;
        }// end function

        private function updateInfo() : void
        {
            var _loc_1:String = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:Array = null;
            var _loc_8:Array = null;
            var _loc_9:int = 0;
            _loc_1 = FashionUtil.getFashionNameByType(this._fashionType);
            title = _loc_1 + Language.getString(41284);
            this._isSureActive.text = Language.getString(41285) + _loc_1;
            var _loc_4:* = Cache.instance.fashion.getFashionInfo(this._fashionType);
            if (_loc_4.baseInfo)
            {
                _loc_2 = _loc_4.baseInfo.openPropNum;
                _loc_5 = FashionUtil.getAdvanceTypeByFashionType(_loc_4.type);
                _loc_7 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, _loc_5);
                _loc_7.sort(ItemsUtil.sortItem);
                _loc_8 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, EAdvanceType._EAdvanceTypeAllRoundUp);
                _loc_8.sort(ItemsUtil.sortItem);
                _loc_6 = _loc_7.concat(_loc_8);
                while (_loc_9 < _loc_6.length)
                {
                    
                    if (ItemsUtil.isPrescriOutTime(_loc_6[_loc_9] as ItemData))
                    {
                    }
                    else if (_loc_4.baseInfo.code >= (_loc_6[_loc_9] as ItemData).itemInfo.effect)
                    {
                        _loc_3 = _loc_3 + (_loc_6[_loc_9] as ItemData).itemAmount;
                    }
                    _loc_9++;
                }
            }
            this._activeConsume.text = Language.getStringByParam(41286, _loc_1, _loc_2);
            this._currOwn.text = Language.getStringByParam(41286, _loc_1, _loc_3);
            return;
        }// end function

        override protected function configParams() : void
        {
            super.configParams();
            paddingBottom = 20 + 20;
            paddingLeft = 7;
            paddingRight = 7;
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:FashionAdvanceData = null;
            var _loc_7:Array = null;
            var _loc_8:Array = null;
            var _loc_9:Dictionary = null;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:ItemInfo = null;
            var _loc_13:ShopItemData = null;
            var _loc_14:int = 0;
            var _loc_15:Boolean = false;
            var _loc_16:PlayerSystemModule = null;
            if (event.target == this._confirmBtn)
            {
                _loc_2 = FashionUtil.getAdvanceTypeByFashionType(this._fashionType);
                _loc_6 = Cache.instance.fashion.getFashionInfo(this._fashionType);
                if (_loc_6.baseInfo)
                {
                    _loc_7 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, _loc_2);
                    _loc_7.sort(ItemsUtil.sortItem);
                    _loc_8 = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(EGroup._EGroupStuff, EStuff._EStuffAdvance, EAdvanceType._EAdvanceTypeAllRoundUp);
                    _loc_8.sort(ItemsUtil.sortItem);
                    _loc_3 = _loc_7.concat(_loc_8);
                    _loc_4 = _loc_6.baseInfo.openPropNum;
                    _loc_9 = new Dictionary();
                    _loc_10 = _loc_3.length;
                    while (_loc_11 < _loc_10)
                    {
                        
                        if (ItemsUtil.isPrescriOutTime(_loc_3[_loc_11] as ItemData))
                        {
                        }
                        else if (_loc_6.baseInfo.code >= (_loc_3[_loc_11] as ItemData).itemInfo.effect)
                        {
                            _loc_5 = _loc_5 + (_loc_3[_loc_11] as ItemData).itemAmount;
                            _loc_9[(_loc_3[_loc_11] as ItemData).uid] = (_loc_3[_loc_11] as ItemData).itemAmount;
                            if (_loc_5 >= _loc_4)
                            {
                                _loc_9[(_loc_3[_loc_11] as ItemData).uid] = (_loc_3[_loc_11] as ItemData).itemAmount - _loc_5 + _loc_4;
                                break;
                            }
                        }
                        _loc_11++;
                    }
                    if (_loc_5 >= _loc_4)
                    {
                        GameProxy.fashion.activeFashion(this._autoBuy.selected, _loc_6.type, _loc_9);
                    }
                    else
                    {
                        _loc_12 = ItemConfig.instance.getConfig(int(_loc_6.baseInfo.propConsume));
                        if (this._autoBuy.selected)
                        {
                            _loc_13 = ShopConfig.instance.getFastBuyItemByCode(_loc_12.bind ? (_loc_12.codeUnbind) : (_loc_12.code));
                            _loc_14 = _loc_13.prize * (_loc_4 - _loc_5);
                            _loc_15 = Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGoldBind, _loc_14, null, false);
                            if (_loc_15)
                            {
                                GameProxy.fashion.activeFashion(this._autoBuy.selected, _loc_6.type, _loc_9);
                            }
                            else
                            {
                                MsgManager.showMouseMsg(Language.getString(41287), MsgRollTipsType.msgRollTips3);
                                if (GiftsUtil.isCanUseFreeGift(_loc_12.group, _loc_12.category, _loc_12.type, int.MAX_VALUE, EGiftBag._EGiftBagFix))
                                {
                                    return;
                                }
                                _loc_16 = GameController.playerSystem.view as PlayerSystemModule;
                                if (!_loc_16.isHide)
                                {
                                    BuyGiftItemWin.instance.setBuyData(_loc_12.code, 2, _loc_4 - _loc_5, _loc_16.x + _loc_16.width + 5, _loc_16.y - 3);
                                }
                            }
                        }
                        else
                        {
                            MsgManager.showMouseMsg(Language.getString(41287), MsgRollTipsType.msgRollTips3);
                            if (GiftsUtil.isCanUseFreeGift(_loc_12.group, _loc_12.category, _loc_12.type, int.MAX_VALUE, EGiftBag._EGiftBagFix))
                            {
                                return;
                            }
                            _loc_16 = GameController.playerSystem.view as PlayerSystemModule;
                            if (!_loc_16.isHide)
                            {
                                BuyGiftItemWin.instance.setBuyData(_loc_12.code, 2, _loc_4 - _loc_5, _loc_16.x + _loc_16.width + 5, _loc_16.y - 3);
                            }
                        }
                    }
                }
            }
            if (event.target == this._cancelBtn)
            {
                this.hide();
            }
            return;
        }// end function

        private function onPackItemChangeHandler(param1:Object) : void
        {
            this.updateInfo();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._isSureActive.dispose();
            this._isSureActive = null;
            this._activeConsume.dispose();
            this._activeConsume = null;
            this._currOwn.dispose();
            this._currOwn = null;
            this._autoBuy.dispose();
            this._autoBuy = null;
            this._confirmBtn.dispose();
            this._confirmBtn = null;
            this._cancelBtn.dispose();
            this._cancelBtn = null;
            this._fashionType = 0;
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : FashionActiveTipWin
        {
            if (!_instance)
            {
                _instance = new FashionActiveTipWin;
            }
            return _instance;
        }// end function

    }
}
